<template>
  <div class="department-container">
    <div class="app-container">
      <!--头部-->
      <el-card>
        <!--一整行-->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <!--两列-->
          <el-col :span="20">
            <svg-icon icon-class="home" /> <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <el-col>
                负责人
              </el-col>
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addDepartments('')">添加子部门</el-dropdown-item>
                    <el-dropdown-item>编辑部门</el-dropdown-item>
                    <el-dropdown-item>删除部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown></el-col>
            </el-row>
          </el-col>
        </el-row>
        <!--主体部分,tree-->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--v-slot="scope"
    -->
          <template v-slot="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu :v-slot="scope">
                        <el-dropdown-item @click.native="addDepartments(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="updateDepartments(scope.data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="doDelete(scope.data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!--对话框,利用组件-->
      <el-dialog
        :title="isEdit?'编辑部门':'添加部门'"
        :visible.sync="dialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="80%"
      >
        <dhkDepartments
          v-if="dialogVisible"
          :pid="pid"
          :is-edit="isEdit"
          :origin-list="originList"
          @addUserInfo="addJk"
          @doClear="hdoClose"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { deGua } from '@/utils/index'
import dhkDepartments from '@/views/departments/deptDialog.vue'
export default {
  components: { dhkDepartments },
  data() {
    return {
      list: [],
      dialogVisible: false,
      pid: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 查询企业的部门列表方法
    async loadDepartments() {
      const res = await getDepartments()
      // console.log(res)
      if (res.code === 10000) {
      // 显示成功信息
        this.$message.success(res.message)
        // 将数据绑定到data中
        this.list = deGua(res.data.depts, '')
        console.log(this.list)
        // 找到originList
        console.log(res.data.depts)
        this.originList = res.data.depts.map(item => {
          return {
            id: item.id,
            pid: item.pid,
            name: item.name,
            code: item.code
          }
        })
      // 渲染数据
      } else {
        this.$message.error(res.message)
      }
    },
    // 点击编辑子部门
    updateDepartments(id) {
      // 点击编辑时
      this.isEdit = true
      // 打开对话框
      this.dialogVisible = true
      // 父传子,传id给子组件
      this.pid = id
    },
    // 点击添加子部门
    addDepartments(id) {
      // 显示对话框
      this.dialogVisible = true
      // pid数据绑定
      this.pid = id
      // 点击添加时
      this.isEdit = false
    },
    // 子传父的函数,回调函数
    addJk() {
      // 关闭对话框
      this.dialogVisible = false
      // 刷新页面
      this.loadDepartments()
    },
    // 子传父的函数,回调函数
    hdoClose() {
      // 关闭对话框
      this.dialogVisible = false
    },
    // 点击删除按钮
    doDelete(id) {
      console.log(id)
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartments(id)
        console.log(res)
        if (res.code === 10000) {
        // 显示成功信息
          this.$message.success(res.message)
          // 刷新页面
          this.loadDepartments()
        } else {
          this.$message.error(res.message)
        }
      }).catch(() => {
        // 用户取消退出
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
